import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.*;
//Current Status - The manufacturer method is working. 
//TO DO - Make the other methods return an arraylist for the drop down menu to keep updating.
//TO DO - Make the vendor to be working to.
// TO DO - Insertion,deletion, Update - Add a text area at LAyout.SOUTH

public class QueryGui extends JFrame 
{
	ArrayList<String> values = new ArrayList<String>();// The selected values.


	private JTextField displayField = new JTextField(30);

	JFrame frame = new JFrame();

	private JComboBox c = new JComboBox();

	private JButton b = new JButton("Add item");

	private int count = 0, p = 0;;
	String manuf ="" ;
	String model = "", year= "", description = "",litres, inches= "", engine= "";

	ArrayList<String> temp;
	//ArrayList<String> temp2;

	public static void main(String[] args) 
	{
		new QueryGui();
	}	 

	public QueryGui() 
	{
		ConnectDb connectDb = new ConnectDb();
		Connection connection = connectDb.getDBConnection();

		String [] menu = {"Enter M for Search by Manufacturer.", "Enter V for searching by vendor and part number","Enter I to Insert rows","Enter Q to quit."};

		String option = JOptionPane.showInputDialog(menu); // About to work on this.- take the input from the user to know what he wants.


		//-------------------------------------------------------------------
		final Query q = new Query(connection, connectDb);


		if(option.equals("M"))
		{
			temp  = q.queryManufac();
			int x = temp.size();
			p++;
			for(int i = 0;i<x;i++)
				c.addItem(temp.get(i));
			//----------------------------------------
			displayField.setEditable(false);
			b.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) 
				{					
					helperButton(q);
				}
			});

			c.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					comboHelper(e);	
				}
			});

		}
		else if(option.equals("V"))
		{
			queryVendorPart q2 = new queryVendorPart(connection, connectDb);
		}
		else if(option.equals("I"))
		{
			new Insert(connection,connectDb);
		}
		else if(option.equals("Q"))
		{
			JOptionPane.showMessageDialog(frame,"Thank you! Have a Nice Day!");
			System.exit(0);
		}


		JPanel displayPanel = new JPanel() ; // the upper panel.
		JPanel optionPanel = new JPanel() ; // the 2nd panel which displays the content.

		optionPanel.setLayout(new FlowLayout());
		displayPanel.add(displayField);
		optionPanel.add(c);
		optionPanel.add(b);
		//connectDb.disconnectFromDB();

		frame.setLayout(new BorderLayout());
		frame.add(displayPanel,BorderLayout.NORTH);
		frame.add(optionPanel,BorderLayout.CENTER);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(400, 400);
		frame.setVisible(true);

	}

	public void addManuf(ArrayList<String> al)
	{
		if(count == 0)
		{
			for(int i = 0;i<al.size();i++)
				c.addItem(al.get(i));
		}
		else
			c.addItem("Insert");

	}

	private void helperButton(Query q)
	{
		if(p == 1)
		{	
			//System.out.println("aweaw"+model);
		actionHelper(temp.toArray());
			temp.clear();
			temp = q.queryModel(manuf);
		//	int x2 = temp.size();
			p++;
		
			System.out.println(c.getSelectedItem());
		}
		if(p == 2)
		{	
			temp.clear();
			temp = q.queryYear(model);
			int x2 = temp.size();

			p++;
			//c.removeAllItems();
			for(int i = 0;i<x2;i++)
				c.addItem(temp.get(i));			
		}if(p == 3)
		{	
			temp.clear();
			temp = q.queryModel(manuf);
			int x2 = temp.size();

			p++;
			c.removeAllItems();
			for(int i = 0;i<x2;i++)
				c.addItem(temp.get(i));			
		}if(p == 4)
		{	
			temp.clear();
			temp = q.queryModel(manuf);
			int x2 = temp.size();

			p++;
			c.removeAllItems();
			for(int i = 0;i<x2;i++)
				c.addItem(temp.get(i));			
		}if(p == 5)
		{	
			temp.clear();
			temp = q.queryModel(manuf);
			int x2 = temp.size();

			p++;
			c.removeAllItems();
			for(int i = 0;i<x2;i++)
				c.addItem(temp.get(i));			
		}if(p == 6)
		{	
			temp.clear();
			temp = q.queryModel(manuf);
			int x2 = temp.size();

			p++;
			c.removeAllItems();
			for(int i = 0;i<x2;i++)
				c.addItem(temp.get(i));			
		}
		//		System.out.println("w"+temp.get(1)+ " "+ p+ " "+manuf );
	}

	public void comboHelper(ActionEvent e)
	{
		if(count == 0)
		{
			count++;
			manuf = (String)((JComboBox) e.getSource()).getSelectedItem();
			displayField.setText("Manufacturer Selected:" + (String)((JComboBox) e.getSource()).getSelectedItem());

			//		System.out.println(count);
		}
		else if(count == 1)
		{

			count++;
			model = (String)((JComboBox) e.getSource()).getSelectedItem();
			System.out.println((String)((JComboBox) e.getSource()).getSelectedItem()+"adw");

			displayField.setText("Model Selected:" + (String)((JComboBox) e.getSource()).getSelectedItem());
		}
		else if(count == 2)
		{
			count++;
			description = (String)((JComboBox) e.getSource()).getSelectedItem();
			displayField.setText("Manufacturer Selected:" + (String)((JComboBox) e.getSource()).getSelectedItem());
		}else if(count == 3)
		{
			count++;
			litres = (String)((JComboBox) e.getSource()).getSelectedItem();
			displayField.setText("Manufacturer Selected:" + (String)((JComboBox) e.getSource()).getSelectedItem());
		}else if(count == 4)
		{
			count++;
			engine = (String)((JComboBox) e.getSource()).getSelectedItem();
			displayField.setText("Manufacturer Selected:" + (String)((JComboBox) e.getSource()).getSelectedItem());
		}
		else if(count == 5)
		{
			count++;
			inches = (String)((JComboBox) e.getSource()).getSelectedItem();
			displayField.setText("Manufacturer Selected:" + (String)((JComboBox) e.getSource()).getSelectedItem());
		}
		else 
			displayField.setText("itemsSelected: " + manuf+","+model);

		//	}
		//	}
		//else
		//	displayField.setText("index: " + c.getSelectedIndex() + "   "+ ((JComboBox) e.getSource()).getSelectedItem());

	}
	
	public void actionHelper(Object[] objects)
	{
		c = new JComboBox(objects);
		c.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				comboHelper(e);	
			}
		});
		}
}

